package com.kmxd.ams.app.event.copy.command;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.kmxd.ams.client.oss.IEfileRecordSvc;
import com.kmxd.ams.client.oss.dto.EfileRecordQueryDTO;
import com.kmxd.ams.client.oss.vo.EfileRecordListVO;
import com.kmxd.ams.core.common.enums.ArchOpEnum;
import com.kmxd.ams.core.oss.template.OssTemplate;
import com.kmxd.ams.core.util.ConvertUtil;
import com.kmxd.ams.infra.arrange.entity.Document;
import com.kmxd.ams.infra.oss.entity.EfileRecord;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 电子文件复制操作
 *
 * @author nickbi
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class FileCopyCmd {

  private final IEfileRecordSvc efileRecordSvc;
  private final OssTemplate ossTemplate;

  @Transactional(rollbackFor = Exception.class)
  public void copy(Document old, Document newDoc, @NotNull final Integer opType) throws Exception {
    log.debug("start copy electronic file old:{}, new:{}", old, newDoc);
    EfileRecordQueryDTO req = new EfileRecordQueryDTO();
    req.setArchId(old.getId());
    req.setArchTypeId(old.getArchTypeId());
    List<EfileRecordListVO> list = efileRecordSvc.selectList(req);
    if (null == list || list.isEmpty()) {
      return;
    }
    for (EfileRecordListVO file : list) {
      // 检查文件是否存在
      // 通过状态，获取复制条目的文件路径信息
      Long oldFileId = file.getId();
      // 通过电子文件整理标识，获取路径信息
      String arrangeFilePath = ossTemplate.getArrangeFilePath(file.getArrangeStatus());
      if (!ossTemplate.exist(arrangeFilePath + file.getPath())) {
        log.debug("copy electronic file, file not exist: {}", file.getPath());
        continue;
      }
      file.setId(null);
      String oldPath = file.getPath();
      String oldFileName = file.getSaveFileName();
      String format = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);
      String newFileName = format + "." + file.getExt();
      // 管理路径{collection/manage}/文档表名/时间/文档id/文件名
      String newFilePath = oldPath.replace(old.getTableId(), newDoc.getTableId()).replace(oldFileName,newFileName);

      file.setPath(newFilePath);
      file.setSaveFileName(newFileName);
      // 复制信息时，新建文件备份，移动不复制电子文件
      log.debug("copy electronic file, old file:{}, new file:{}", oldPath, file.getPath());
      ossTemplate.copy(
          arrangeFilePath + oldPath,
          ossTemplate.getCollectPath(file.getDefPath()) + file.getPath());

      EfileRecord efileRecord = ConvertUtil.toBean(file, EfileRecord.class);
      efileRecord.setArchId(newDoc.getId());
      efileRecord.setArchTypeId(newDoc.getArchTypeId());
      efileRecord.setFondsId(newDoc.getFondsId());
      efileRecord.setMoveStatus(0L);
      efileRecord.setArrangeStatus(0);
      efileRecordSvc.save(efileRecord);
      if (opType.equals(ArchOpEnum.MOVE.getCode())) {
        efileRecordSvc.remove(String.valueOf(oldFileId));
      }
    }
  }
}
